﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using XISoft.AppCreator.Util;
using XISoft.AppCreator.Util.Common;

namespace XISoft.AppCreator.CommonUI.UML
{
    public partial class ItemPanel : UserControl
    {
        private readonly List<Control> children = new List<Control>();

        private readonly ItemHeader header;
        public ItemPanel(ClassSon classson, ClassObject classobject)
        {
            InitializeComponent();

            SuspendLayout();
            this.DoubleBuffered = true;
            _classobject = classobject;
            _classson = classson;
            this.Width = classobject.Width-2;

            this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                                    | System.Windows.Forms.AnchorStyles.Right)));

            header = new ItemHeader(classson, this);
            header.ContextMenuStrip = popAddOject;
            this.Controls.Add(header);


            switch (classson)
            {
                case ClassSon.Event:
                    popObject.Text = "事件"; this.popCon.Visible = false; this.popSplit.Visible = false;
                    break;
                case ClassSon.Method:
                    popObject.Text = "方法"; this.popCon.Visible = false; this.popSplit.Visible = false;
                    break;
                case ClassSon.Attribute:
                    popObject.Text = "属性";
                    this.popCon.Visible = false; this.popSplit.Visible = false;
                    break;
                case ClassSon.Filed:
                    popObject.Text = "字段";
                    break;
            }
            ResumeLayout();
            children.Add(header);
            
        }



        public ItemPanel(ClassSon classson, ClassObject classobject,bool isConstuction,string caption)
            :this(classson,classobject)
        {

            //Item item = null;
            //if (isConstuction)
            //{
            //    item = new Item(classson, caption, this);
            //    item.Name = caption;
            //}
            //else
            //{
            //    item = new Item(_classson,caption, this);
            //}
            //this.Controls.Add(item);

            //item.Location = new Point(0, header.Bottom);
            //this.Size = new Size(_classobject.Width - 2, item.Bottom+1);

           // this.Refresh(); _classobject.Refresh();
        }

        public ItemPanel(ClassSon classson, ClassObject classobject, string caption)
            : this(classson, classobject)
        {

            //Item item = new Item(classson, caption, this);
            //item.Name = caption;

            //this.Controls.Add(item);

            //item.Location = new Point(0, header.Bottom);
            //this.Size = new Size(_classobject.Width - 2, item.Bottom + 1);

           // this.Refresh(); 
          //  _classobject.Refresh();
        }

        private ClassSon _classson;
        private ClassObject _classobject = null;


        private void popObject_Click(object sender, EventArgs e)
        {
            AddItem(false,string.Empty,_classson);
        }
        private void ItemPanel_Load(object sender, EventArgs e)
        {
            //this.Height = Bottom;

            for (int i = 1; i < children.Count; i++)
                children[i].Top = children[i - 1].Bottom;
            this.Size = new Size(_classobject.Width - 2, children[children.Count - 1].Bottom);
        }

        private bool _isExpand = true;
        public Boolean isExpand
        {
            get { return _isExpand; }
        }

        public void change()
        {
            _isExpand = !_isExpand;

            if (_isExpand)
                this.Height = children[children.Count - 1].Bottom;
            else
                this.Height = 17;//Controls[0].Bottom;
            this.Refresh();
            Parent.Refresh();
        }

        public void AddItem(bool isConstruction, string _caption, ClassSon _classType)
        {
            if (String.IsNullOrEmpty(_caption)) _caption = _classType.ToString();
            int id=-1;
            DataRow dr=null;

            #region rename
            List<Item> list = new List<Item>();
            Boolean finded = false;
            int index = 1;
            String _name=_caption;
            foreach (Control c in Controls)
            {
                if (c is Item && (c as Item).ClassSon == _classType)
                    list.Add(c as Item);
            }
            do
            {
                finded = false;
                foreach (Item item in list)
                {
                    if (item.Name == _name)
                    {
                        _name = _caption + "_" + index;
                        index++;
                        finded = true;
                        list.Remove(item);
                        break;
                    }

                }
            }while (finded);
            _caption = _name;
            #endregion

            if (!isConstruction)
            {
                if (_classType == ClassSon.Attribute || _classType == ClassSon.Method)
                {

                    _classobject.entity.Rows.Add(dr = _classobject.entity.NewRow());

                    dr[ModelDataDictionary.EntityTable.Type] = _classType.ToString();

 
                    id = _classobject.entity.Rows.IndexOf(dr);
                }
                Item item = new Item(_classson,_caption, this,id);
                this.Controls.Add(item);
                children.Add(item);
                item.Location = new Point(0, children[children.Count - 2].Bottom);

                this.Size = new Size(_classobject.Width - 2, item.Bottom);

                _isExpand = true;

                if (_classType == ClassSon.Attribute || _classType == ClassSon.Method)
                {
                    dr[ModelDataDictionary.EntityTable.Name] = _caption;
                    dr[ModelDataDictionary.EntityTable.Code] = _caption;
                    dr[ModelDataDictionary.EntityTable.DataType] = item.Type;
                    item.ItemChange += new EventHandler(item_ItemChange);
                }
            }
            else
            {
                if (!hasObject(_caption) && (_classType == ClassSon.Method))
                {

                        _classobject.entity.Rows.Add(dr = _classobject.entity.NewRow());

                        dr[ModelDataDictionary.EntityTable.Type] = _classType.ToString();


                        id = _classobject.entity.Rows.IndexOf(dr);


                    Item item = new Item(_classType, _caption, this,id);
                    item.Name = _caption;
                    this.Controls.Add(item);
                    children.Add(item);
                    item.Location = new Point(0, children[children.Count - 2].Bottom);

                    this.Size = new Size(_classobject.Width - 2, item.Bottom);
                    _isExpand = true;
                    item.ItemChange += new EventHandler(item_ItemChange);

                    dr[ModelDataDictionary.EntityTable.Name] = _caption;
                    dr[ModelDataDictionary.EntityTable.DataType] = item.Type;
                }
            }
        }

        public void AddItem(bool isConstruction, string _caption, ClassSon _classType,int id)
        {
            if (String.IsNullOrEmpty(_caption)) _caption = _classType.ToString();

            #region rename
            List<Item> list = new List<Item>();
            Boolean finded = false;
            int index = 1;
            String _name = _caption;
            foreach (Control c in Controls)
            {
                if (c is Item && (c as Item).ClassSon == _classType)
                    list.Add(c as Item);
            }
            do
            {
                finded = false;
                foreach (Item item in list)
                {
                    if (item.Name == _name)
                    {
                        _name = _caption + "_" + index;
                        index++;
                        finded = true;
                        list.Remove(item);
                        break;
                    }

                }
            } while (finded);
            _caption = _name;
            #endregion

            if (!isConstruction)
            {

                Item item = new Item(_classson, _caption, this, id);
                this.Controls.Add(item);
                children.Add(item);
                item.Location = new Point(0, children[children.Count - 2].Bottom);


                this.Size = new Size(_classobject.Width - 2, item.Bottom);

                _isExpand = true;

                if (_classType == ClassSon.Attribute)
                {

                    item.ItemChange += new EventHandler(item_ItemChange);
                }
            }
            else
            {
                if (!hasObject(_caption) && (_classType == ClassSon.Method))
                {

                    Item item = new Item(_classType, _caption, this, id);
                    item.Name = _caption;
                    this.Controls.Add(item);
                    children.Add(item);
                    item.Location = new Point(0, children[children.Count - 2].Bottom);

                    this.Size = new Size(_classobject.Width - 2, item.Bottom);
                    _isExpand = true;
                    item.ItemChange += new EventHandler(item_ItemChange);
                }
            }
        }

        void item_ItemChange(object sender, EventArgs e)
        {
            Item item=sender as Item;
            _classobject.entity.Rows[item.ID][ModelDataDictionary.EntityTable.Name] = item.Name;
            _classobject.entity.Rows[item.ID][ModelDataDictionary.EntityTable.DataType] = item.Type;
        }


        public void Remove(Control c)
        {
            Item item = c as Item;
            _classobject.entity.Rows.RemoveAt(item.ID);
            _classobject.init();

        }

        private void popAddOject_Opening(object sender, CancelEventArgs e)
        {

        }

        private bool hasObject(string ctrlName)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.Name == ctrlName)
                {
                    return true;
                }
            }
            return false;
        }


        private void popDelete_Click(object sender, EventArgs e)
        {
            _classobject.Controls.Remove(this);
            _classobject.Refresh();
            for (int i = Controls.Count - 1; i >= 0; i--)
            {
                Control c = Controls[i];
                if (c is Item)
                {
                    Item item = c as Item;
                    _classobject.entity.Rows.RemoveAt(item.ID);
                }
                Controls.RemoveAt(i);
                c.Dispose();
            }
            this.Dispose();
        }

        
       // public void GetCon
    }
}
